package com.rainy.guoran.business.manager.controller;

import com.rainy.guoran.business.manager.config.SwaggerProperties;
import com.rainy.guoran.business.manager.entity.CommonResult;
import com.rainy.guoran.business.manager.entity.UserInfoVO;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/userInfo")
@Tag(
        name = "UserInfoControllerAPI",
        description = "用户控制器接口",
        externalDocs = @ExternalDocumentation(
                description = "这是一个接口文档介绍",
                url = "https://rainy.guoran.com/"))
public class UserInfoController {

    /***
     * 根据ID查询用户信息（单条）
     * @param id 用户id
     * @return 返回一条数据
     */
    @Operation(
            summary = "根据Id查询用户信息",
            description = "根据ID查询用户信息，并返回响应结果信息",
            parameters = {
                    @Parameter(name = "id", description = "用户ID", required = true, example = "1")
            },
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "响应成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            title = "CommonResult和UserInfoVO组合模型",
                                            description = "返回实体，CommonResult内data为UserInfoVO模型",
                                            anyOf = {CommonResult.class, UserInfoVO.class})
                            )
                    ),
                    @ApiResponse(
                            responseCode = "500",
                            description = "响应失败",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            title = "CommonResult模型",
                                            description = "返回实体，CommonResult内data为空",
                                            implementation = CommonResult.class)
                            )
                    )
            }
    )
    @GetMapping("/getUserInfo/{id}")
    public CommonResult<UserInfoVO> getUserInfoById(@PathVariable(value = "id") Long id) {
        //模拟用户数据
        List<String> likes = Arrays.asList("码代码", "看电影", "玩游戏");
        UserInfoVO UserInfoVO = new UserInfoVO(id, "Rainy", 30, "上海", likes);
        return new CommonResult<>(200, "成功", UserInfoVO);
    }

    /***
     * 查询全部用户数据
     * @return 返回数据
     */
    @Operation(
            summary = "查询全部用户数据",
            description = "查询用户信息，并返回响应结果信息",
            parameters = {},
            deprecated = true,
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "响应成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            title = "CommonResult和UserInfoVO组合模型",
                                            description = "返回实体，CommonResult内data为UserInfoVO模型(并且UserInfoVO为集合)",
                                            anyOf = {CommonResult.class, UserInfoVO.class})
                            )
                    )
            }
    )
    @GetMapping("/findAll")
    public CommonResult<List<UserInfoVO>> findAllUserInfo() {
        //模拟用户数据
        List<String> likes = Arrays.asList("码代码", "看电影", "玩游戏");
        UserInfoVO userInfo1 = new UserInfoVO(1L, "Rainy1", 30, "上海浦东", likes);
        UserInfoVO userInfo2 = new UserInfoVO(2L, "Rainy2", 31, "上海松江", likes);
        return new CommonResult<>(200, "成功", Arrays.asList(userInfo1, userInfo2));
    }

    /***
     * 用户添加接口
     * @param userInfoVO 用户DTO信息
     * @return 成功信息
     */
    @Operation(
            summary = "用户添加接口",
            description = "用户添加接口",
            parameters = {},
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "用户信息DTO",
                    required = true,
                    content = {
                            @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(implementation = UserInfoVO.class)
                            )
                    }
            ),
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "响应成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            title = "CommonResult模型",
                                            description = "返回实体CommonResult，并且Data为null",
                                            implementation = CommonResult.class)
                            )
                    )
            }
    )
    @PostMapping("/saveUserInfo")
    public CommonResult<UserInfoVO> saveUserInfo(@RequestBody UserInfoVO userInfoVO) {
        System.out.println("成功添加数据：" + userInfoVO);
        return new CommonResult<>(200, "成功", userInfoVO);
    }

    @Resource
    SwaggerProperties swaggerProperties;

    @GetMapping
    public String getProperties(){
        System.out.println(swaggerProperties);
        return swaggerProperties.getInfo().getTitle();
    }
}

